//
//  PPSettingLocationPicker.m
//  pickpic
//
//  Created by GaoYongyue on 16/10/1.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPSettingLocationPicker.h"
#import "PPSettingManager.h"

@interface PPSettingLocationPicker ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) DGPopoverView *popoverView;
@property (nonatomic, strong) UIView *aView;
@property (nonatomic, strong) NSArray *provinces;
@property (nonatomic, strong) NSArray *cityArray;
@property (nonatomic, strong) NSArray *cityDataSource;

@property (nonatomic, strong) NSString *defaultProvince;
@property (nonatomic, strong) NSString *defaultCity;
@end
@implementation PPSettingLocationPicker
- (void)awakeFromNib
{
    [super awakeFromNib];

    self.provinces = [PPSettingManager sharedInstance].provinceArray;
    self.cityArray = [PPSettingManager sharedInstance].cityArray;

    self.cityDataSource = [self.cityArray safeObjectAtIndex:0];
}

- (IBAction)cancleButtonDidPress:(id)sender {
    [_popoverView dismissAnimated:YES completion:nil];
}
- (IBAction)confirmButtonDidPress:(id)sender {
    __weak typeof(self)weakSelf = self;
    NSInteger index1 = [_pickerView selectedRowInComponent:0];
    NSInteger index2 = [_pickerView selectedRowInComponent:1];
    NSDictionary *obj1 = [_provinces safeObjectAtIndex:index1];
    NSDictionary *obj2 = [_cityDataSource safeObjectAtIndex:index2];
    NSString *selectedProvince = [[obj1 validDictionary] validStringForKey:@"name"];
    NSString *selectedCity = [[obj2 validDictionary] validStringForKey:@"name"];
    [_popoverView dismissAnimated:YES completion:^{
        if ([selectedProvince isEqualToString:weakSelf.defaultProvince] && [selectedCity isEqualToString:weakSelf.defaultCity])
        {
            return ;
        }
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:weakSelf.aView animated:YES];
        [PPNetworkService updateProfileWithName:nil gender:nil country:nil state:selectedProvince city:selectedCity birthday:nil signature:nil type:3 callback:^(id data, NSError *error) {
            [hud hide:YES];
            if (data)
            {
                [weakSelf.aView toastMessage:@"变更成功" duration:1.f];
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didUpdateLocationSuccessfull:newCity:)])
                {
                    [weakSelf.delegate didUpdateLocationSuccessfull:selectedProvince newCity:selectedCity];
                }
            }
            else
            {
                [weakSelf.aView toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }];
}

- (void)showInView:(UIView *)view
{
    _aView = view;
    self.width = view.width;
    self.bottom = view.height;

    _popoverView = [[DGPopoverView alloc] initWithContainerView:view contentView:self options:DGPopoverViewAnimationOptionUpward backgroundStyle:DGPopoverViewBackgroundStyleDimmed];
    [_popoverView displayAnimated:YES completion:nil];
}

- (void)setDefaultState:(NSString *)state city:(NSString *)city
{
    _defaultProvince = state;
    _defaultCity = city;
    __block NSInteger index = -1;
    [_provinces enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *name = [[obj validDictionary] validStringForKey:@"name"];
        if ([name isEqualToString:state]) {
            index = idx;
            *stop = YES;
        }
    }];

    __block NSInteger index1 = -1;
    NSArray *cityDataSource = [self.cityArray safeObjectAtIndex:index];
    [cityDataSource enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *name = [[obj validDictionary] validStringForKey:@"name"];
        if ([name isEqualToString:city]){
            index1 = idx;
            *stop = YES;
        }
    }];
    if (index >=0 && index1 >= 0)
    {
        self.cityDataSource = cityDataSource;
        [_pickerView selectRow:index inComponent:0 animated:NO];
        [_pickerView selectRow:index1 inComponent:1 animated:NO];
        [_pickerView reloadAllComponents];
    }
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
        return [_provinces count];
    }
    else if (component == 1)
    {
        return [_cityDataSource count];
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        self.cityDataSource = [self.cityArray safeObjectAtIndex:row];
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView reloadComponent:1];
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        NSDictionary *obj = [_provinces safeObjectAtIndex:row];
        NSString *province = [[obj validDictionary] validStringForKey:@"name"];
        return province;
    }
    else if (component == 1)
    {
        NSDictionary *obj = [[_cityDataSource safeObjectAtIndex:row] validDictionary];
        NSString *city = [[obj validDictionary] validStringForKey:@"name"];
        return city;
    }
    return nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
